package com.jwpdk.tagframework;

import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.jwpdk.appframework.data.DataMap;
import com.jwpdk.appframework.data.DatasetList;
import com.jwpdk.appframework.data.IData;
import com.jwpdk.appframework.data.IDataList;
import com.jwpdk.common.Constant;
import com.jwpdk.dbframework.SqlMap;
import com.jwpdk.util.StringUtils;

/**
 * 下拉列表  test 后期集成UI库
 * @author 王志伟
 *
 */
public class Select extends JwpdkTag {

	private static final long	serialVersionUID	= 2994254596958533536L;

	private JspWriter			jspOut				= null;

	// ========接收变量====================================================
	private String				name				= null;				// 下拉框的名字[必须]
	private String				id					= null;				// 下拉框的id[必须]
	private String				value				= null;				// 下拉框的初始值
	private String				code				= null;				// 下拉框初始数据源映射
	private String				param				= null;				// 参数
	private String				cellTrans			= "VALUE|TEXT";		// 格式化code字段,value|text
	private IDataList			dataList			= null;				// 对象传递
	private String				staticData			= null;				// 静态数据源[text="男",value="1"],[text="男",value="1"]
	private String				isNull				= "true";				// true,增加请选择
	private String				readonly			= "false";				// 是否只读,文本框替换
	private String				noShow				= "false";				// 不显示,隐藏域替换
	private String				height				= null;
	private String				width				= null;
	private String				style				= null;
	private String				onblur				= null;
	private String				onchange			= null;
	private String				onclick				= null;
	private String				ondblclick			= null;
	private String				onfocus				= null;
	private String				onkeydown			= null;
	private String				onkeypress			= null;
	private String				onmousedown			= null;
	private String				onmousemove			= null;
	private String				onmouseout			= null;
	private String				onmouseup			= null;
	private String				onselect			= null;

	@Override
	public int doStartTag() throws JspException {
		jspOut = pageContext.getOut();
		// HttpServletRequest request = (HttpServletRequest)
		// pageContext.getRequest();
		// StringBuilder stringBuilder = new StringBuilder();
		// stringBuilder.append(potjs(request));
		// stringBuilder.append(nkUtiljs(request));//加载nkjs
		// if(request.getAttribute("nokat_tag_KLFSD78213HDBN87921FCZXSKUWE_CONT")==null){
		// stringBuilder.append(stylebut(request));//class样式
		// request.setAttribute("nokat_tag_KLFSD78213HDBN87921FCZXSKUWE_CONT",
		// "y");
		// }
		// stringBuilder.append(javaScript());//javascript
		// stringBuilder.append(ListInit(options(request)));
		try {

			jspOut.write(getSelect(getOptions()));
		} catch ( Exception e ) {
			throw new JspException(e.getMessage());
		}
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * 获取最终的select组件
	 * @param iDataList
	 * @return
	 */
	private String getSelect(IDataList iDataList) {
		StringBuffer sbf = new StringBuffer();
		// id name
		StringBuffer idname = new StringBuffer();
		String valueCell = StringUtils.getArrayFromString(cellTrans,"|")[0].toUpperCase();
		String textCell = StringUtils.getArrayFromString(cellTrans,"|")[1].toUpperCase();

		idname.append("id='").append(StringUtils.trim(id)).append("' name='").append(StringUtils.trim(name)).append("'");
		if ( "true".equalsIgnoreCase(noShow) || "true".equalsIgnoreCase(readonly) ) { // 隐藏
			sbf.append("<input ").append(idname).append(" type='hidden' value='").append(StringUtils.trim(value)).append("' />\n");
			if ( "true".equalsIgnoreCase(readonly) ) {// 只读
				sbf.append("<input type='text' readonly value='");
				for (int i = 0; i < iDataList.size(); i++) {
					if ( StringUtils.trim(value).equals(iDataList.get(i,valueCell)) ) {
						sbf.append(iDataList.get(i,textCell));
						break;
					}
				}
				sbf.append("' ").append(getAttributeAndEventString()).append(" />\n");
			}
		}
		if ( sbf.length() > 0 )
			return sbf.toString();

		sbf.append("<select ").append(idname).append(" ").append(getAttributeAndEventString()).append(" > \n");
		if ( "true".equalsIgnoreCase(isNull) ) {
			sbf.append("\t<option value='' >---请选择---</option> \n");
		}
		if ( iDataList != null && iDataList.size() > 0 ) {
			for (int i = 0; i < iDataList.size(); i++) {
				if(StringUtils.IsEmptyOrNull(iDataList.get(i,valueCell))) continue;
				sbf.append("\t<option value='").append(iDataList.get(i,valueCell)).append("' ");
				if ( StringUtils.trim(value).equals(iDataList.get(i,valueCell)) )
					sbf.append(" selected ");
				sbf.append(">").append(iDataList.get(i,textCell)).append("</option>\n");
			}
		}
		sbf.append("</select>\n");
		return sbf.toString();
	}

	/**
	 * 获取属性及事件串
	 * @return
	 */
	private StringBuffer getAttributeAndEventString() {
		StringBuffer attrEvent = new StringBuffer();
		if ( !StringUtils.IsEmptyOrNull(height) )
			attrEvent.append("height='").append(StringUtils.trim(height)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(width) )
			attrEvent.append("width='").append(StringUtils.trim(width)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(style) )
			attrEvent.append("style='").append(StringUtils.trim(style)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onblur) )
			attrEvent.append("onblur='").append(StringUtils.trim(onblur)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onchange) )
			attrEvent.append("onchange='").append(StringUtils.trim(onchange)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onclick) )
			attrEvent.append("onclick='").append(StringUtils.trim(onclick)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(ondblclick) )
			attrEvent.append("ondblclick='").append(StringUtils.trim(ondblclick)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onfocus) )
			attrEvent.append("onfocus='").append(StringUtils.trim(onfocus)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onkeydown) )
			attrEvent.append("onkeydown='").append(StringUtils.trim(onkeydown)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onkeypress) )
			attrEvent.append("onkeypress='").append(StringUtils.trim(onkeypress)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onmousedown) )
			attrEvent.append("onmousedown='").append(StringUtils.trim(onmousedown)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onmousemove) )
			attrEvent.append("onmousemove='").append(StringUtils.trim(onmousemove)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onmouseout) )
			attrEvent.append("onmouseout='").append(StringUtils.trim(onmouseout)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onmouseup) )
			attrEvent.append("onmouseup='").append(StringUtils.trim(onmouseup)).append("' ");
		if ( !StringUtils.IsEmptyOrNull(onselect) )
			attrEvent.append("onselect='").append(StringUtils.trim(onselect)).append("' ");

		return attrEvent;
	}

	/**
	 * 将集合中的key转换为大写
	 * @param iDataList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private IDataList transKeyToUpperCase(IDataList iDataList) {
		IDataList rsDataList = new DatasetList();
		IData rsData;
		for (int i = 0; i < iDataList.count(); i++) {
			rsData = new DataMap();
			IData ids = (IData) iDataList.get(i);
			String key;
			for (Iterator iter = ids.keySet().iterator(); iter.hasNext();) {
				key = (String) iter.next();
				rsData.put(key.toUpperCase(),ids.get(key));
			}
			rsDataList.add(rsData);
		}
		return rsDataList;
	}

	/**
	 * 组织列表数据
	 * @return
	 */
	private IDataList getOptions() throws Exception {
		if ( dataList != null ) {
			return transKeyToUpperCase(dataList);
		}
		if ( !StringUtils.IsEmptyOrNull(staticData) && staticData.indexOf("]") != -1 ) {
			return transKeyToUpperCase(StringUtils.strToIDataList(staticData));
		}
		IDataList iDataList = null;

		SqlMap sqlMap = new SqlMap(Constant.TAG_TRANSACTION);
		iDataList = sqlMap.iSelect(code,StringUtils.strToIData(param));

		return iDataList;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNoShow() {
		return noShow;
	}

	public void setNoShow(String noShow) {
		this.noShow = noShow;
	}

	public String getOnblur() {
		return onblur;
	}

	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getOndblclick() {
		return ondblclick;
	}

	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getOnkeydown() {
		return onkeydown;
	}

	public void setOnkeydown(String onkeydown) {
		this.onkeydown = onkeydown;
	}

	public String getOnkeypress() {
		return onkeypress;
	}

	public void setOnkeypress(String onkeypress) {
		this.onkeypress = onkeypress;
	}

	public String getOnmousedown() {
		return onmousedown;
	}

	public void setOnmousedown(String onmousedown) {
		this.onmousedown = onmousedown;
	}

	public String getOnmousemove() {
		return onmousemove;
	}

	public void setOnmousemove(String onmousemove) {
		this.onmousemove = onmousemove;
	}

	public String getOnmouseout() {
		return onmouseout;
	}

	public void setOnmouseout(String onmouseout) {
		this.onmouseout = onmouseout;
	}

	public String getOnmouseup() {
		return onmouseup;
	}

	public void setOnmouseup(String onmouseup) {
		this.onmouseup = onmouseup;
	}

	public String getOnselect() {
		return onselect;
	}

	public void setOnselect(String onselect) {
		this.onselect = onselect;
	}

	public String getReadonly() {
		return readonly;
	}

	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getIsNull() {
		return isNull;
	}

	public void setIsNull(String isNull) {
		this.isNull = isNull;
	}

	public String getStaticData() {
		return staticData;
	}

	public void setStaticData(String staticData) {
		this.staticData = staticData;
	}

	// ===================================================================

	public IDataList getDataList() {
		return dataList;
	}

	public void setDataList(IDataList dataList) {
		this.dataList = dataList;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public String getCellTrans() {
		return cellTrans;
	}

	public void setCellTrans(String cellTrans) {
		this.cellTrans = cellTrans;
	}
}
